const { imgUrl } = getApp().globalData;
import { mineApi } from "@/utils/request/api/mine";
Page({
  data: {
    active: 0,
    imgUrl: imgUrl + "mine/",
    refresherStatus: false,
    hasMore: true,
    page: 1,
    page_size: 10,
    total: 0,
    list: [],
  },
  // 上下拉加载
  // 触底
  scrolltolower() {
    if (!this.data.hasMore) {
      return;
    }
    let page = this.data.page + 1;
    this.setData({
      page,
    });
    this.getList();
  },
  // 下拉刷新
  handleRefresherRefresh() {
    this.setData({
      refresherStatus: true,
      page: 1,
      hasMore: true,
    });
    this.getList();
  },
  getList() {
    mineApi
      .getOrderList({
        page: this.data.page,
        page_size: this.data.page_size,
      })
      .then((res) => {
        let arr = res.data.list;
        console.log("arr === ", arr);
        this.setData({
          list: this.data.page == 1 ? arr : this.data.list.concat(arr),
          total: res.data.total,
          refresherStatus: false,
          hasMore: this.data.page * this.data.page_size <= res.data.total,
        });
      });
  },
  backMoney(event) {
    const { index } = event.currentTarget.dataset;
    const { id } = this.data.list[index];
    mineApi.applyRefund({
      order_id: id, remark: ''
    }).then(res => {
      console.log('res === ', res);
      wx.showToast({
        title: '退款成功',
        icon: 'none',
        duration: 2000,
        mask: true,
      });
      this.getList();
    })
  },
  onLoad(options) {},
  changeTab(event) {
    let active = event.currentTarget.dataset.current;
    this.setData({
      active,
    });
  },
  toApply(event) {
    const { index } = event.currentTarget.dataset;
    wx.navigateTo({
      url: "/pages/mine/spendingRecords/apply/apply",
      success: res => {
        res.eventChannel.emit('apply', { data: this.data.list[index] });
      }
    });
  },
  toPay(event) {
    const { index } = event.currentTarget.dataset;
    wx.navigateTo({
      url: "/pages/mine/spendingRecords/prePay/prePay",
      success: res => {
        res.eventChannel.emit('apply', { data: this.data.list[index] });
      }
    });
  },
  onReady() {},
  onShow() {
    this.getList();
  },
  onHide() {},
  onUnload() {},
  onShareAppMessage() {
    return {
      title: "",
    };
  },
});
